﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace QunProject.Common
{
    /// <summary>
    /// 缓存日志
    /// </summary>
    public static class LogCache
    {
        /// <summary>
        /// 获取一个缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="cacheTime">过期时间（秒）</param>
        /// <param name="directory">路径</param>
        /// <returns></returns>
        public static string GetCache(string key, int cacheTime = -1, string directory = "")
        {
            string text = HttpContext.Current.Server.MapPath("/") + "Cache";
            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = text;
            }
            FileInfo fileInfo = new FileInfo(directory + "\\" + key + ".cache");
            string result;
            if (fileInfo.Exists)
            {
                if (cacheTime == -1 || fileInfo.LastWriteTime.AddSeconds((double)cacheTime) > DateTime.Now)
                {
                    result = File.ReadAllText(directory + "\\" + key + ".cache");
                }
                else
                {
                    result = "";
                }
            }
            else
            {
                result = "";
            }
            return result;
        }
        /// <summary>
        /// 设置一个缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="directory">路径</param>
        public static void SetCache(string key, string value, string directory = "")
        {
            string text = HttpContext.Current.Server.MapPath("/") + "Cache";
            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = text;
            }
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            else if (File.Exists(directory + "\\" + key + ".cache"))
            {
                File.Delete(directory + "\\" + key + ".cache");
            }
            if (value != "")
            {
                File.WriteAllText(directory + "\\" + key + ".cache", value);
            }
        }
        /// <summary>
        /// 删除一个缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="directory">路径</param>
        public static void DeleteCache(string key, string directory = "")
        {
            string text = HttpContext.Current.Server.MapPath("/") + "Cache";
            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = text;
            }
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            File.Delete(directory + "\\" + key + ".cache");
        }
        /// <summary>
        /// 清理所有缓存
        /// </summary>
        /// <param name="directory">路径</param>
        public static void CleanCache(string directory = "")
        {
            string text = HttpContext.Current.Server.MapPath("/") + "Cache";
            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = text;
            }
            Directory.Delete(directory, true);
            Directory.CreateDirectory(directory);
        }
        /// <summary>
        /// 设置一个日志
        /// </summary>
        /// <param name="content">日志内容</param>
        /// <param name="directory">路径</param>
        public static void WriteLog(string content, string directory = "")
        {
            string text = HttpContext.Current.Server.MapPath("/") + "Log";
            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = text;
            }
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            string txtPath = directory + "\\" + DateTime.Now.ToShortDateString() + ".log";
            using (StreamWriter sw = new StreamWriter(txtPath, true))
            {
                sw.WriteLine(content + sw.NewLine);
                sw.Close();
            }
        }
        /// <summary>
        /// 清理所有日志
        /// </summary>
        /// <param name="directory">路径</param>
        public static void CleanLog(string directory = "")
        {
            string text = HttpContext.Current.Server.MapPath("/") + "Log";
            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = text;
            }
            Directory.Delete(directory, true);
            Directory.CreateDirectory(directory);
        }
    }

}
